﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Threading;
using com.fukefu.info.blog;
using com.fukefu.sql;
using com.fukefu.tools;

namespace com.fukefu.update.instantiated
{
    public class SinaBlogPost : UpdateData
    {
        //函数
        SqlServerOperate sql = new SqlServerOperate();
        //变量
        Queue<WebUrl> queue = new Queue<WebUrl>();
        SinaBlog sblog = new SinaBlog();
        string FIDCollection = "";
        SinaArticle article = null;
        Random random = new Random();
        //开始工作
        public override void Start()
        {
            SetMaxNum(1000);
            sblog._dispaly += Display;
            Display("系统启动...");
            article = new SinaArticle();
            while (true)
            {
                //注册账号
                Register();
                //抓取内容
                InitLink();
                //发布20条
                for (int i = 0; i < 15; i++)
                {
                    while (queue.Count < 5)
                    {
                        Display("连接发布完了,休息10分钟");
                        //发布完了，休息十分钟
                        Thread.Sleep(10 * 60 * 1000);
                        //抓取内容
                        InitLink();
                    }
                    string html = GetArticle();
                    string title = Regex.Replace(html, "<.+?>", "").Substring(15, 17);
                    if (sblog.Post(title, html, "股票 杂谈"))
                    {
                        Display(title + "  发布成功");
                        UpdateSinaPostTimes();
                        SetNumAdd();
                    }
                    else
                    {
                        Display(title + "  发布失败");
                    }
                    Thread.Sleep(70000 + random.Next(30000));
                }
                Display("这个账户发布结束了,休息10分钟");
                Display("------------------------------");
                //发布完了，休息十分钟
                Thread.Sleep(10 * 60 * 1000);
                Display("启动新账户");
            }
        }
        //注册
        private void Register()
        {
            string name = "", pass = "";
            do
            {
                name = "ff" + MyFunction.CreateMobileNum();
                pass = MyFunction.CreatePassword(9);
                Thread.Sleep(15 * 1000);
            } while (!sblog.Register(name, pass));
        }
        //加载发布的内容
        private void InitLink()
        {
            queue.Clear();
            Display("加载网址中...");
            string cmd = "select top 800 a.fid,a.url,b.word,a.statuscode from 已发布 as a,_bt_关键字 as b where (a.sina_dt is null or a.sina_oneday<2  or datediff(d,b.dt,getdate())!=0) and a.shoulu=0 and DATEDIFF(D,b.dt,GETDATE())<7 and a.kid=b.kid order by NEWID()";
            SqlDataReader re = sql.Query(cmd);
            while (re.Read())
            {
                WebUrl web = new WebUrl();
                web.Uid = re["fid"].ToString();
                web.Url = re["url"].ToString();
                web.Word = re["word"].ToString();
                web.StatusCode = Convert.ToInt32(re["statuscode"]);
                queue.Enqueue(web);
            }
            re.Close();
            Display("开始发布...");
        }
        //获取文章
        private string GetArticle()
        {
            List<string> list = new List<string>();
            for (int i = 0; i < 20; i++)
            {
                if (queue.Count > 1)
                {
                    list.Add(CreateLink());
                }
            }
            return article.Get(list);
        }
        //更新使用次数
        private void UpdateSinaPostTimes()
        {
            string[] p = FIDCollection.Split('|');
            foreach (string _p in p)
            {
                sql.Update("exec _sina_update_times '" + _p + "'");
            }
            FIDCollection = "";
        }
        //创建连接 如果状态码不对,更新
        private string CreateLink()
        {
            string link = "";
            WebUrl web = null;
            do
            {
                web = queue.Dequeue();
                //正常页面
                if (web.StatusCode == 200)
                {
                    link = "<a target=\"_blank\" href=\"" + web.Url + "\">" + web.Word + "</a>";
                }
                else
                {
                    //非正常页面更新状态码
                    int id = source.GetHttpStatusCode(web.Url);
                    if (id != web.StatusCode)
                    {
                        sql.Update("update 已发布 set statuscode=" + id + " where fid='" + web.Uid + "'");
                        web.StatusCode = id;
                        if (id == 200)
                        {
                            link = "<a target=\"_blank\" href=\"" + web.Url + "\">" + web.Word + "</a>";
                        }
                    }
                }
                //增加需要更新的ID
                if (web != null)
                {
                    FIDCollection += (FIDCollection.Equals("") ? "" : "|") + web.Uid;
                }
            } while (web != null && web.StatusCode != 200);
            return link;
        }

        public override string GetTitle()
        {
            return "新浪博客";
        }
    }
}
